const jobsService = require('../services/jobsService')


exports.getAllJobs = async (req,res) =>{
    try {
        const jobs = await jobsService.getAllJobs();
        res.json(jobs);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

exports.getOnlyJob = async (req,res) =>{
    try {
        const jobs = await jobsService.findJob(req.query);
        res.json(jobs);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

exports.createJob = async (req,res) =>{
    try {
        const newJob = await jobsService.createJob(req.body);
        res.status(201).json(newJob);
    }catch(error) {
        res.status(400).json({error:error.message});
    }
};

exports.getJobById = async (req,res) =>{
    try {
        const jobById = await jobsService.getJobById(req.params.id);
        res.json(jobById);
    } catch (error) {
        res.status(404).json({error:error.message});
    }
};

exports.updateJob = async (req,res) =>{
    try {
        const updatedJob = await jobsService.updateaJob(req.params.id,req.body);
        res.json(updatedJob);
    } catch (error) {
        const statusCode = error.message === 'Job not found' ? 404 : 400;
        res.status(statusCode).json({error:error.message});
    }
};


exports.deleteJob = async (req,res) =>{
    try {
        await jobsService.deleteJob(req.params.id);
        res.status(204).send();
    } catch (error) {
        res.status(404).json({error: error.message});
    }
};


exports.filterJobs = async (req,res) => {
    try {
    const rows = await jobsService.filtrateJobs(req.query || {});
        res.json(rows);
    } catch (err) {
        res.status(500).json({error:err.message});
    }
}
